package io.github.talelin.latticy.controller.v1.ag;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.dto.ag.MonitoringObject.CreateOrUpdateMonitoringObjectDTO;
import io.github.talelin.latticy.model.ag.MonitoringObejctDO;
import io.github.talelin.latticy.service.ag.MonitoringObjectService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@Validated
@RequestMapping("/v1/ag/MonitoringObject")
public class MonitoringObjectController {
    @Autowired
    private MonitoringObjectService moService;

    @GetMapping("/{id}")
    public MonitoringObejctDO getMO(@PathVariable("id") @Positive(message = "id必须为正整数") int id){
        MonitoringObejctDO mo =moService.getById(id);
        if (mo==null){
            throw new NotFoundException("未找到被监控的对象",10023);
        }
        return mo;
    }
    @GetMapping("/list")
    public List<MonitoringObejctDO> getMOs(){
        List<MonitoringObejctDO> mos = moService.getMonitoringObejcts();
        if (mos==null){
            throw new NotFoundException("未找到被监控的对象",10023);
        }
        return mos;
    }
    @PostMapping("/create")
    public CreatedVO createMo(@RequestBody @Validated CreateOrUpdateMonitoringObjectDTO validator){
        moService.createRoom(validator);
        return new CreatedVO("创建监控对象成功");
    }
    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除监控对象", module = "监控对象", mount = true)
    @GroupRequired
    public DeletedVO deletedMo(@PathVariable("id") @Positive(message = "{id}")int id){

        MonitoringObejctDO mo = moService.getById(id);
        if (mo==null){
            throw new NotFoundException("MonitoringObejct not found",10022);
        }
        moService.deleteById(mo.getId());
        return new DeletedVO(16);
    }
    @PutMapping("/{id}")
    public UpdatedVO updateMo(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                              @RequestBody @Validated CreateOrUpdateMonitoringObjectDTO validator){
        MonitoringObejctDO mo = moService.getById(id);
        if (mo == null){
            throw new NotFoundException("building not found",10022);
        }
        moService.updateMonitoringObject(mo,validator);
        return new UpdatedVO("更新成功");
    }
}
